﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;
    using WMS6.DALFactory;

    public class UdfReportSettingBLL : IBaseInfo
    {
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private PubParasInfo pubParasInfo;
        private static readonly IUdfReportSettingDA udfReportSettingDA = DataAccess.CreateUdfReportSettingDA();
        private static readonly IUdfReportSettingDetailDA udfReportSettingDetailDA = DataAccess.CreateUdfReportSettingDetailDA();

        public UdfReportSettingBLL()
        {
            this.pubParasInfo = null;
        }

        public UdfReportSettingBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public UdfReportSettingBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string Delete(List<string> IDS)
        {
            string temp = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                int i;
                List<UdfReportSettingInfo> udfReportSettingList = new List<UdfReportSettingInfo>();
                UdfReportSettingInfo udfReportSettingInfo = null;
                for (i = 0; i < IDS.Count; i++)
                {
                    udfReportSettingInfo = udfReportSettingDA.GetUdfReportSettingByID(dataBase, tran, IDS[i], this.dataBaseID[0]);
                    udfReportSettingList.Add(udfReportSettingInfo);
                }
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                udfReportSettingDetailDA.DeleteUdfReportSettingDetail(dataBase, tran, IDS, this.dataBaseID[0]);
                udfReportSettingDA.DeleteUdfReportSetting(dataBase, tran, IDS, this.dataBaseID[0]);
                for (i = 0; i < IDS.Count; i++)
                {
                    auditDataBLL.InsertAuditData<UdfReportSettingInfo>(dataBase, tran, udfReportSettingList[i], null, AuditAction.Delete, this.pubParasInfo.UserID, "UDF_REPORT_SETTING", this.dataBaseID);
                    if ((udfReportSettingList[i].UdfReportSettingDetailInfos != null) && (udfReportSettingList[i].UdfReportSettingDetailInfos.Count > 0))
                    {
                        for (int j = 0; j < udfReportSettingList[i].UdfReportSettingDetailInfos.Count; j++)
                        {
                            auditDataBLL.InsertAuditData<UdfReportSettingDetailInfo>(dataBase, tran, udfReportSettingList[i].UdfReportSettingDetailInfos[j], null, AuditAction.Delete, this.pubParasInfo.UserID, "UDF_REPORT_SETTING_DETAIL", this.dataBaseID[0]);
                        }
                    }
                    temp = temp + IDS[i] + ";";
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            if (temp.Length > 0)
            {
                return temp.Substring(0, temp.Length - 1);
            }
            return "";
        }

        public DataSet GetAllInfo()
        {
            return udfReportSettingDA.GetAllUdfReportSetting(this.dataBaseID[0]);
        }

        public object GetInfoByID(string id)
        {
            UdfReportSettingInfo udfReportSettingInfo = udfReportSettingDA.GetUdfReportSettingByID(id, this.dataBaseID[0]);
            if (udfReportSettingInfo != null)
            {
                udfReportSettingInfo.UdfReportSettingDetailInfos = udfReportSettingDetailDA.GetUdfReportSettingDetailListByID(id, this.dataBaseID[0], this.pubParasInfo);
            }
            return udfReportSettingInfo;
        }

        public DataSet GetInfoByQueryEntity(object obj)
        {
            UdfReportSettingQueryEntity udfReportSettingQueryEntity = obj as UdfReportSettingQueryEntity;
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(udfReportSettingQueryEntity.ReportID))
            {
                sqlWhere.Add(" REPORT_ID " + udfReportSettingQueryEntity.ReportID);
            }
            if (!string.IsNullOrEmpty(udfReportSettingQueryEntity.ReportFile))
            {
                sqlWhere.Add(" REPORT_FILE " + udfReportSettingQueryEntity.ReportFile);
            }
            if (!string.IsNullOrEmpty(udfReportSettingQueryEntity.ReportName))
            {
                sqlWhere.Add(" REPORT_NAME " + udfReportSettingQueryEntity.ReportName);
            }
            return udfReportSettingDA.GetUdfReportSettingByQueryList(sqlWhere, udfReportSettingQueryEntity, this.dataBaseID[0]);
        }

        public string Insert(object obj)
        {
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            UdfReportSettingInfo udfReportSettingInfo = obj as UdfReportSettingInfo;
            try
            {
                if (string.IsNullOrEmpty(udfReportSettingInfo.ReportID))
                {
                    udfReportSettingInfo.ReportID = commonMethod.GenNextNumStr(dataBase, tran, "UDF_REPORT_SETTING", this.dataBaseID[0]);
                }
                else if (!string.IsNullOrEmpty(udfReportSettingInfo.ReportID) && !udfReportSettingDA.CheckUdfReportSettingIDUnique(dataBase, tran, udfReportSettingInfo.ReportFile, this.dataBaseID[0]))
                {
                }
                udfReportSettingDA.InsertUdfReportSetting(dataBase, tran, udfReportSettingInfo, this.dataBaseID[0]);
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                auditDataBLL.InsertAuditData<UdfReportSettingInfo>(dataBase, tran, null, udfReportSettingInfo, AuditAction.Insert, this.pubParasInfo.UserID, "UDF_REPORT_SETTING", this.dataBaseID);
                if ((udfReportSettingInfo.UdfReportSettingDetailInfos != null) && (udfReportSettingInfo.UdfReportSettingDetailInfos.Count > 0))
                {
                    for (int i = 0; i < udfReportSettingInfo.UdfReportSettingDetailInfos.Count; i++)
                    {
                        if (udfReportSettingDetailDA.CheckUdfReportSettingDetailIDUnique(dataBase, tran, udfReportSettingInfo.UdfReportSettingDetailInfos[i].ReportID, udfReportSettingInfo.UdfReportSettingDetailInfos[i].LineID.ToString(), this.dataBaseID[0]))
                        {
                            PublicMethod.GetInstance().ShowAlertMessage("F1870_001", this.pubParasInfo.UserLanguage, new List<string> { udfReportSettingInfo.UdfReportSettingDetailInfos[i].ReportID, udfReportSettingInfo.UdfReportSettingDetailInfos[i].LineID.ToString() });
                        }
                        else
                        {
                            udfReportSettingInfo.UdfReportSettingDetailInfos[i].CreatedDate = DateTime.Now;
                            udfReportSettingInfo.UdfReportSettingDetailInfos[i].UpdatedDate = DateTime.Now;
                            udfReportSettingInfo.UdfReportSettingDetailInfos[i].CreatedBy = this.pubParasInfo.UserID;
                            udfReportSettingInfo.UdfReportSettingDetailInfos[i].UpdatedBy = this.pubParasInfo.UserID;
                            udfReportSettingDetailDA.InsertUdfReportSettingDetail(dataBase, tran, udfReportSettingInfo.UdfReportSettingDetailInfos[i], this.dataBaseID[0]);
                            auditDataBLL.InsertAuditData<UdfReportSettingDetailInfo>(dataBase, tran, null, udfReportSettingInfo.UdfReportSettingDetailInfos[i], AuditAction.Insert, this.pubParasInfo.UserID, "UDF_REPORT_SETTING_DETAIL", this.dataBaseID[0]);
                        }
                    }
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return udfReportSettingInfo.ReportID;
        }

        public string Update(object obj)
        {
            string strReportID = string.Empty;
            try
            {
                UdfReportSettingInfo udfReportSettingInfo = obj as UdfReportSettingInfo;
                UdfReportSettingInfo oldudfreportsettingInfo = udfReportSettingDA.GetUdfReportSettingByID(udfReportSettingInfo.ReportID, this.dataBaseID[0]);
                strReportID = udfReportSettingInfo.ReportID;
                if (udfReportSettingDA.UpdateUdfReportSetting(udfReportSettingInfo, this.dataBaseID[0]) > 0)
                {
                    new AuditDataBLL().InsertAuditData<UdfReportSettingInfo>(oldudfreportsettingInfo, udfReportSettingInfo, AuditAction.Update, this.pubParasInfo.UserID, "UDF_REPORT_SETTING", this.dataBaseID[0]);
                }
            }
            catch (Exception ex)
            {
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            return strReportID;
        }
    }
}

